package com.genericdevice.platform.core.data;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.genericdevice.platform.core.define.DBQueries;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Xilent on 18.05.2014.
 */
public class DataBase extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "genericmeasurement";

    public static final String PLUGINS_TABLE = "gnm_plugins";

    public static final String PLUGINS_NAME_FIELD = "name";
    public static final String PLUGINS_DESC_FIELD = "description";
    public static final String PLUGINS_PATH_FIELD = "path";
    public static final String PLUGINS_CLASS_FIELD = "class";
    public static final String PLUGINS_DEFAULT_FIELD = "default_plugin";

    private static DataBase INSTANCE;

    private String query;

    public static void initializeDB(Context context) {
        INSTANCE = new DataBase(context);
    }

    public static DataBase getInstance() {
        return INSTANCE;
    }

    private DataBase(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(String.format(DBQueries.CREATE_DB_QUERY, PLUGINS_TABLE, PLUGINS_NAME_FIELD, PLUGINS_DESC_FIELD, PLUGINS_PATH_FIELD, PLUGINS_CLASS_FIELD, PLUGINS_DEFAULT_FIELD));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public void setQuery(QueryHelper query) {
        setQuery(query.toString());
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public void updateDB() {
        SQLiteDatabase db = getWritableDatabase();
        assert db != null;
        db.execSQL(query);
    }

    public List<Map<String, Object>> loadResult(Map<String, Class<?>> format) {
        SQLiteDatabase db = getReadableDatabase();
        assert db != null;
        Cursor cursor = db.rawQuery(query, new String[]{});

        List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
        while (cursor.moveToNext()) {
            Map<String, Object> value = new HashMap<String, Object>();

            for (Map.Entry<String, Class<?>> entry : format.entrySet()) {
                if (entry.getValue().equals(Integer.class)) {
                    value.put(entry.getKey(), cursor.getInt(cursor.getColumnIndex(entry.getKey())));
                } else if (entry.getValue().equals(Double.class)) {
                    value.put(entry.getKey(), cursor.getDouble(cursor.getColumnIndex(entry.getKey())));
                } else if (entry.getValue().equals(Float.class)) {
                    value.put(entry.getKey(), cursor.getFloat(cursor.getColumnIndex(entry.getKey())));
                } else if (entry.getValue().equals(String.class)) {
                    value.put(entry.getKey(), cursor.getString(cursor.getColumnIndex(entry.getKey())));
                }
            }

            values.add(value);
        }

        return values;
    }
}
